package com.example.Utils;


import java.nio.charset.Charset;

public class CommonStringUtils {
    public static boolean isNullOrEmpty(String str) {
        return str == null ? true : "".equals(str);
    }

    public static boolean isNotNullOrEmpty(String str) {
        return str != null && !"".equals(str);
    }

    /**
     * 将字符串转为double
     *
     * @param str
     * @return
     */
    public static Double safeDouble(String str) {
        if (isNullOrEmpty(str)) {
            return 0.00;
        }
        double res = 0.00;
        try {
            res = Double.valueOf(str);
        } catch (Exception e) {
        }
        return res;
    }

    /**
     * 字符串转为Integer
     *
     * @param str
     * @return
     */
    public static Integer safeInteger(String str) {
        if (isNullOrEmpty(str)) {
            return 0;
        }
        int res = 0;
        try {
            res = Integer.valueOf(str);
        } catch (Exception e) {
        }
        return res;
    }

    /**
     * 转为字符串
     * @param obj
     * @return
     */
    public static String safeString(Object obj){
        if(obj == null){
            return "";
        }
        return obj.toString();
    }

    public static String encodeString(String str,String charset){
        Charset charset1 = Charset.forName(charset);
        str = new String(str.getBytes(Charset.defaultCharset()),charset1);
        return str;
    }
}
